using UnityEngine;
using Photon.Pun;
using UnityEngine.UI;

public class PlayerNameCaseUI : MonoBehaviourPun, IPunObservable
{
    public string PlayerName
    {
        get { return PlayerName; }
        set
        {
            if (PlayerName != value)
            {
                PlayerName = value;
                if (nameText != null)
                {
                    nameText.text = PlayerName;
                }
            }
        }
    }
    public int PlayerRoomIndex
    {
        get
        {
            return PlayerRoomIndex;
        }
        set
        {
            if (PlayerRoomIndex != value)
            {
                PlayerRoomIndex = value;
                if (roomIndexText != null)
                {
                    roomIndexText.text = PlayerRoomIndex.ToString("D");
                }
            }
        }
    }
    [SerializeField] private Text nameText;
    [SerializeField] private Text roomIndexText;

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.IsWriting)
        {
            stream.SendNext(PlayerName);
            stream.SendNext(PlayerRoomIndex);
        }
        // 对于其他玩家，接收数据并保存
        else
        {
            this.PlayerName = (string)stream.ReceiveNext();
            this.PlayerRoomIndex = (int)stream.ReceiveNext();
        }
    }
}
